﻿using System;
using System.Collections.Generic;

using SBPweb.Communication.Shared.Library.Contacts;

using SBPweb.Communication.Client.Library.Xmpp.Commands;

namespace SBPweb.Communication.Client.Library.Xmpp.Contacts
{

    partial class ContactManager : ContactManagerBase
    {

        #region Private fields

        private agsXMPP.protocol.client.PresenceManager presenceManager;

        #endregion

        #region Methods

        public override void GetContactList()
        {
            messenger.Commands.SendCommand(new GetRosterCommand(connections.localJid));
        }

        #region Event handlers

        #region Life cycle

        protected override void OnInitialize()
        {
            presenceManager = new agsXMPP.protocol.client.PresenceManager(connections.client);

            connections.client.AutoRoster = Configuration.GetContactListWhenConnected;

            connections.client.OnRosterStart += new agsXMPP.ObjectHandler(OnClientRosterStart);
            connections.client.OnRosterItem += new agsXMPP.XmppClientConnection.RosterHandler(OnClientRosterItem);
            connections.client.OnRosterEnd += new agsXMPP.ObjectHandler(OnClientRosterEnd);

            connections.client.OnPresence += new agsXMPP.protocol.client.PresenceHandler(OnClientPresence);

            connections.Connecting += new EventHandler(OnClientConnecting);
            connections.Connected += new EventHandler(OnClientConnected);
        }

        protected override void OnTerminate()
        {
            connections.Connected -= new EventHandler(OnClientConnected);
            connections.Connecting -= new EventHandler(OnClientConnecting);

            connections.client.OnPresence -= new agsXMPP.protocol.client.PresenceHandler(OnClientPresence);

            connections.client.OnRosterStart -= new agsXMPP.ObjectHandler(OnClientRosterStart);
            connections.client.OnRosterItem -= new agsXMPP.XmppClientConnection.RosterHandler(OnClientRosterItem);
            connections.client.OnRosterEnd -= new agsXMPP.ObjectHandler(OnClientRosterEnd);

            presenceManager = null;
            //rosterManager = null;
        }

        #endregion

        #region Roster items

        private void OnClientRosterStart(object sender)
        {
            contacts.Clear();
            OnContactListReceiving(sender, EventArgs.Empty);
        }

        private void OnClientRosterEnd(object sender)
        {
            OnContactListReceived(sender, EventArgs.Empty);
        }

        private void OnClientRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
        {
            switch (item.Subscription)
            {
                default:
                case agsXMPP.protocol.iq.roster.SubscriptionType.none:
                    break;
                case agsXMPP.protocol.iq.roster.SubscriptionType.both:
                    contacts.Add(new Contact(item));
                    break;
            }
        }

        #endregion

        #region Presence changes

        private void OnClientPresence(object sender, agsXMPP.protocol.client.Presence pres)
        {
            if (pres.From.Equals(connections.localJid))
            {
                return;
            }
            Contact contact = GetContactById(pres.From.Bare) as Contact;
            if (contact != null)
            {
                contact.PresenceState = new ContactPresenceState(pres);
                OnContactPresenceChanged(sender, new ContactPresenceStateBaseEventArgs(contact, contact.PresenceState));
            }
        }
        
        #endregion

        #endregion

        #endregion

    }

}